package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class AddLoginInfo extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		/* First let us create the LoginInfo Object */
		String lUserName = req.getParameter("select_box");
		String lPassword = req.getParameter("password");
		System.out.println("Username was " + lUserName + " Password is " + lPassword);
		if( (lUserName == null) || (lPassword == null))
		{
			System.out.println("[ERROR] Username or Password was null");
			return;
		}
		
		CLoginInfo lLoginInfo = CUserFactory.createNewLoginInfo(lUserName, lPassword);
		if( lLoginInfo == null )
		{
			System.out.println("DEPARTMENT password already set <BR>");
			lLoginInfo = CUserFactory.getUserLoginInfo(lUserName);
		}
		PersistenceManager pm = PMF.get().getPersistenceManager();
		lLoginInfo.setmPassword(lPassword);
		pm.makePersistent(lLoginInfo);
		pm.close();
		
		String lMessage = "Password for " + lUserName + " has been set.";
		req.setAttribute("message", lMessage);
		req.setAttribute("USER", lUserName);
		try
		{
			req.getRequestDispatcher("/modules/administration/users.jsp").forward(req,resp);
		}catch(Exception e) 
		{ 
			e.printStackTrace(); 
		}
		
	}
}
